require("SOD/model/DrawModel")

module SOD
  
  class DrawConstructionModel < DrawModel
    
    def initialize
      super
    end
    
    def draw(bar, active_model)
      super
      
      levelGroup = Sketchup.active_model.active_entities.add_group
      levelGroup.name = bar.name + "_Level1"
      levelEnts = levelGroup.entities
      
      levelGroup.move! @origin
      rotation = Geom::Transformation.rotation(@origin, [0, 0, 1], @angle)
      levelGroup.transform! rotation
      
      face = levelEnts.add_face([0, 0, 0], [@length, 0, 0], [@length, @width, 0], [0, @width, 0])
      face.reverse!
      face.pushpull(@level_height)
      
      levelGroup.entities.each { |e| Plugin.sodLayers.putOnConstructionLayer(e) }
      
      @active_model.add levelGroup
      
      copyToStoreys(levelGroup, bar.name + "_Level")
      
      conGroup = Sketchup.active_model.active_entities.add_group
      conGroup.name = bar.name + "_Slab1"
      cons = conGroup.entities
      
      conGroup.move! @origin
      tran = Geom::Transformation.translation [0, 0, @overall_height]
      conGroup.transform! tran
      conGroup.transform! rotation
      
      constructionArea = cons.add_face([0, 0, 0], [@length, 0, 0], [@length, @width, 0], [0, @width, 0])
      constructionArea.pushpull @construction_height 
      
      conGroup.entities.each { |c| c.material = "SOD_Steel" if c.typename == "Face" }
    
      @active_model.add conGroup
      
      copyToStoreys(conGroup, bar.name + "_Slab")
      
      self.drawGrid(bar, active_model) if @genes
      
    end
    
    def drawGrid(bar, active_model)
      getGridValues(bar, active_model)
      createGrid(bar, active_model)
    end
    
    def getGridValues(bar, active_model)
      
      @numOfColumns = @genes[1]
      @part = @length/(@numOfColumns+1)
      @positionOfColumns = @hash[:ps_col].m
    
    end
    
    def createGrid(bar, active_model)
      gridGroup = ents.add_group
      gridGroup.name = bar.name + "-Grid"
      gridEnts = gridGroup.entities
      
      gridGroup.move! @origin
      rotation = Geom::Transformation.rotation(@origin, [0, 0, 1], @angle)
      gridGroup.transform! rotation
	
      for i in 1..@numOfColumns
	for j in 0...@levels
	  #outer column rows
	  gridEnts.add_cline([@part*i,0,@overall_height*j], [@part*i,@width,@overall_height*j], "_") # Li puts beam upon column -> FR:put on ground again!
	  vg = gridEnts.add_cline([@part*i,0,@overall_height*j], [@part*i,0,(@overall_height*j)+@level_height])
	  vg.stipple = "."
	  vg = gridEnts.add_cline([@part*i,@width,@overall_height*j], [@part*i,@width,(@overall_height*j)+@level_height])
	  vg.stipple = "."
	  
    puts "gridEnts"
	  #inner column row
	  gridEnts.add_cline([0,@positionOfColumns,@overall_height*j], [@length,@positionOfColumns,@overall_height*j], "_") # Li puts beam upon column -> FR: put on ground again!
	  gridEnts.add_cline([0,@positionOfColumns,@overall_height*j], [0,@positionOfColumns,(@overall_height*j)+@level_height], ".")
	  gridEnts.add_cline([@part*i,@positionOfColumns,@overall_height*j], [@part*i,@positionOfColumns,(@overall_height*j)+@level_height], ".")
	  gridEnts.add_cline([@length,@positionOfColumns,@overall_height*j], [@length,@positionOfColumns,(@overall_height*j)+@level_height], ".")
	end
      end
      
      if Sketchup.active_model.rendering_options["HideConstructionGeometry"] == true 
	Sketchup.active_model.rendering_options["HideConstructionGeometry"] = false 
      end
      
      @active_model.add gridGroup
    end
    
  end
  
end
